package com.zte.ychr.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;

public class EntityUtils {

	/**
	 * 得到实体类里面的属性值（方法）
	 * @param model
	 * @param name
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getValue(Object model, String name)  {
		try {
			name = name.substring(0, 1).toUpperCase() + name.substring(1); // 将属性的首字符大写，方便构造get，set方法
			Method method = model.getClass().getMethod("get"+name);
			return (T) method.invoke(model);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getValue(Object model, String name, Class<T> clazz)  {
		Object obj = getValue(model, name);
		if (obj == null) {
			return null;
		}
		return (T)obj;
	}
	
	public static String getString(Object model, String name) {
		return getValue(model, name, String.class);
	}
	
	public static Long getLong(Object model, String name)  {
		return getValue(model, name, Long.class);
	}
	
	public static String getDatePattern(Object model, String name) {
		Field field = null;
		try {
			field = model.getClass().getDeclaredField(name);
		} catch(NoSuchFieldException ex) {
			try {
				field = model.getClass().getSuperclass().getDeclaredField(name);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if (field != null) {
			JsonFormat jsonFormat = field.getAnnotation(JsonFormat.class);
			if (jsonFormat != null) {
				return jsonFormat.pattern();
			}
		}
		
		return "yyyy-MM-dd";
	}
	
	/**
	 * 设置实体类里面的属性值（方法）
	 * @param model
	 * @param name
	 * @param value
	 */
	public static void setValue(Object model, String name, Object value) {
		try {
			Field field = model.getClass().getDeclaredField(name);
			name = name.substring(0, 1).toUpperCase() + name.substring(1); // 将属性的首字符大写，方便构造get，set方法
			
			String type = field.getGenericType().toString();
			
			if (type.equals("class java.lang.String")) { // 如果type是类类型，则前面包含"class "，后面跟类名
				Method m = model.getClass().getMethod("set"+name,String.class);
			    m.invoke(model, value);
			}else if (type.equals("class java.lang.Long")) {
				Method m = model.getClass().getMethod("set"+name,Long.class);
			    m.invoke(model, value);
			}else if (type.equals("class java.lang.Integer")) {
				Method m = model.getClass().getMethod("set"+name,Integer.class);
			    m.invoke(model, value);
			}else if (type.equals("class java.lang.Boolean")) {
				Method m = model.getClass().getMethod("set"+name,Boolean.class);
			    m.invoke(model, value);
			}else if (type.equals("class java.util.Date")) {
				Method m = model.getClass().getMethod("set"+name,Date.class);
			    m.invoke(model, value);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static boolean compareFieldValue(Object source,Object target,String fieldName){
		try {
			if (getValue(source, fieldName)==null&&getValue(target, fieldName)==null) {
				return true;
			}
			return getValue(source, fieldName).equals(getValue(target, fieldName));
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 
	}
	//不可以比较日期或者字段自定义类型的字段，只能比较常见类型可以转化为String的属性
	public static boolean compareFieldValue(Object source,Object target,String sourceFieldName,String targeFieldName){
		try {
			if (getValue(source, sourceFieldName)==null&&getValue(target, targeFieldName)==null) {
				return true;
			}
			Object value = getValue(source, sourceFieldName);
			String string = value!=null?value.toString():"";
			String anObject = getValue(target, targeFieldName)+"";
			return string.equals(anObject);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 
	}
	public static void copyFieldValue(Object source,Object target,String fieldName){
		try {
			setValue(target, fieldName, getValue(source, fieldName));
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 
	}
	
}
